package com.hds.modules.app.entity.wx.po;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hds.modules.app.entity.wx.common.WxConstants;
import com.hds.modules.app.entity.wx.utils.WxPayUtils;

/**
 * 统一下单微信返回数据类
 * @version 1.0
 */
public class WxUnifiedOrderResult {
    /**
     * 【必填】
     * 返回状态码
     * 此字段是通信标识，非交易标识，交易是否成功需要查看result_code来判断
     */
    private String return_code = "";

    /**
     * 【非必填】
     * 返回信息
     */
    private String return_msg = "";
    //return_code为SUCCESS才会有下面数据
    /**
     * 【必填】
     * 公众账号ID
     */
    private String appId = "";

    /**
     * 【必填】
     * 商户号
     */
    private String mch_id = "";

    /**
     * 【非必填】
     * 设备号
     */
    private String device_info = "";

    /**
     * 【必填】
     * 随机字符串
     */
    private String nonce_str = "";

    /**
     * 【必填】
     * 签名
     */
    private String sign = "";

    /**
     * 【必填】
     * 业务结果
     */
    private String result_code = "";

    /**
     * 【非必填】
     * 错误代码
     */
    private String err_code = "";

    /**
     * 【非必填】
     * 错误代码描述
     */
    private String err_code_des = "";

    //result_code为SUCCESS才会有下面数据

    /**
     * 【必填】
     * 交易类型
     */
    private String trade_type = "";

    /**
     * 【必填】
     * 预支付交易会话标识
     */
    private String prepayId = "";

    /**
     * 【非必填】
     * 二维码链接
     */
    private String code_url = "";


    public String getReturn_code() {
        return return_code;
    }
    public void setReturn_code(String return_code) {
        this.return_code = return_code;
    }
    public String getReturn_msg() {
        return return_msg;
    }
    public void setReturn_msg(String return_msg) {
        this.return_msg = return_msg;
    }
    public String getAppId() {
        return appId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
    public String getMch_id() {
        return mch_id;
    }
    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }
    public String getDevice_info() {
        return device_info;
    }
    public void setDevice_info(String device_info) {
        this.device_info = device_info;
    }
    public String getNonce_str() {
        return nonce_str;
    }
    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }
    public String getResult_code() {
        return result_code;
    }
    public void setResult_code(String result_code) {
        this.result_code = result_code;
    }
    public String getErr_code() {
        return err_code;
    }
    public void setErr_code(String err_code) {
        this.err_code = err_code;
    }
    public String getErr_code_des() {
        return err_code_des;
    }
    public void setErr_code_des(String err_code_des) {
        this.err_code_des = err_code_des;
    }
    public String getPrepayId() {
        return prepayId;
    }
    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }
    public String getTrade_type() {
        return trade_type;
    }
    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }
    public String getCode_url() {
        return code_url;
    }
    public void setCode_url(String code_url) {
        this.code_url = code_url;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    /**
     * 解析统一下单返回数据
     * @param resultXML
     * @return
     */
    public WxUnifiedOrderResult getWxUnifiedOrderResult(String resultXML,String apiKey)
    {
        List<String> paramNameList = new ArrayList<String>();

        Map<String,String> paramValueMap = new HashMap<String,String>();

        paramValueMap.put(WxConstants.APIKEY, apiKey);

        try {
            //构建xml文档对象
            Document doc = DocumentHelper.parseText(resultXML);

            //获取xml根元素
            Element root = doc.getRootElement();
            //获取return_code节点
            Element return_codeE = root.element("return_code");
            //获取return_msg节点
            Element return_msgE = root.element("return_msg");

            String return_code = return_codeE==null?WxConstants.FAIL:return_codeE.getTextTrim();

            String return_msg = return_msgE==null?"":return_msgE.getTextTrim();

            this.setReturn_code(return_code);

            this.setReturn_msg(return_msg);

            //如果return_code 为 SUCCESS
            if(WxConstants.SUCCESS.equals(return_code))
            {
                Element appidE = root.element("appid");
                Element mch_idE = root.element("mch_id");
                Element device_infoE = root.element("device_info");
                Element nonce_strE = root.element("nonce_str");
                Element signE = root.element("sign");
                Element result_codeE = root.element("result_code");
                Element err_codeE = root.element("err_code");
                Element err_code_desE = root.element("err_code_des");

                String appid = appidE==null?"":appidE.getTextTrim();
                String mch_id = mch_idE==null?"":mch_idE.getTextTrim();
                String device_info = device_infoE==null?"":device_infoE.getTextTrim();
                String nonce_str = nonce_strE==null?"":nonce_strE.getTextTrim();
                String sign = signE==null?"":signE.getTextTrim();
                String result_code = result_codeE==null?"":result_codeE.getTextTrim();
                String err_code = err_codeE==null?"":err_codeE.getTextTrim();
                String err_code_des = err_code_desE==null?"":err_code_desE.getTextTrim();

                this.setAppId(appid);
                this.setMch_id(mch_id);
                this.setDevice_info(device_info);
                this.setNonce_str(nonce_str);
                this.setSign(sign);
                this.setResult_code(result_code);
                this.setErr_code(err_code);
                this.setErr_code_des(err_code_des);

                //如果 result_code 为SUCCESS
                if(WxConstants.SUCCESS.equals(result_code))
                {
                    Element trade_typeE = root.element("trade_type");
                    Element prepay_idE = root.element("prepay_id");
                    Element code_urlE = root.element("code_url");

                    String trade_type = trade_typeE==null?"":trade_typeE.getTextTrim();
                    String prepay_id = prepay_idE==null?"":prepay_idE.getTextTrim();
                    String code_url = code_urlE==null?"":code_urlE.getTextTrim();

                    this.setTrade_type(trade_type);
                    this.setPrepayId(prepay_id);
                    this.setCode_url(code_url);

                    /*由于微信文档说明可能会随时对返回数据增加字段,所以为了能正确验签要动态获取参加验签的数据*/
                    WxPayUtils.getSignData(root, paramNameList, paramValueMap);
                    String[] paramNames = new String[paramNameList.toArray().length];
                    paramNameList.toArray(paramNames);

                    String checkSign = WxPayUtils.getPaySignMD5(paramNames, paramValueMap);

                    if(!checkSign.equals(this.getSign()))
                    {
                        this.setResult_code(WxConstants.FAIL);
                        this.setErr_code("SIGNERROR");
                        this.setErr_code_des("统一下单返回数据验签失败");
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            this.setReturn_code(WxConstants.FAIL);
            this.setReturn_msg("统一下单返回数据解析失败");
        }
        return this;
    }

}